package com.wjw.service;

import com.wjw.mapper.DamageTypeFtableMapper;
import com.wjw.model.Damage_type_ftable;
import com.wjw.utils.GetSqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 损坏类型父参照表数据库操作:逻辑控制层
 */
@Service
public class DamageTypeFtableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private DamageTypeFtableMapper mapper=sqlsession.getMapper(DamageTypeFtableMapper.class);

    /**
     * 查看所有损坏父类型信息
     * @return
     */
    public List<Damage_type_ftable> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeFtableMapper mapper=sqlsession.getMapper(DamageTypeFtableMapper.class);
        try {
            List<Damage_type_ftable> all = mapper.findAll();
            return all;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    /**
     * 查看所有损坏父类型信息
     * @return
     */
    public List<Damage_type_ftable> findByPage(int page, int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeFtableMapper mapper=sqlsession.getMapper(DamageTypeFtableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Damage_type_ftable> all = mapper.findByPage(start,limit);
            return all;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    /**
     * 添加损坏父类型信息
     * @param e
     */
    public boolean add(Damage_type_ftable e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeFtableMapper mapper=sqlsession.getMapper(DamageTypeFtableMapper.class);
        try {
            mapper.add(e);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeFtableMapper mapper=sqlsession.getMapper(DamageTypeFtableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Damage_type_ftable e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeFtableMapper mapper=sqlsession.getMapper(DamageTypeFtableMapper.class);
        try {
            mapper.change(e);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

}
